package cn.gok.homework;

import java.util.*;
import java.util.stream.Stream;

/*
1 、创建Book类： int bid（编号）,String bname,float price
2 、创建 TreeMap<Book,String> map;加入5本书，
   map.put(new Book(1,"西游记",100),"老白");
要求按书的价格从高到低排序输出。
使用两个遍历方法：  keySet遍历，entrySet遍历
3 、 创建   ArrayList<Book> list;添加5本书，
使用 stream运算，将价格低于150元的书，按编号从低到高输出。
提示    filter(?).sort(new BookCompartor()).forEach(...)
 */
class Book{
    private int bid;
    private String bname;
    private float price;

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }
}
class BookComparable implements Comparator<Book> {
    @Override
    public int compare(Book o1, Book o2) {
        if(o1.getPrice()>o2.getPrice()){
            return -1;
        }else if (o1.getPrice()==o2.getPrice()&&o1.getBname().equals(o2.getBname())){
            return 0;
        }
        return  1;
    }

}
public class Homework {
    public static void main(String[] args) {
        作业二();
        作业三();
    }
/*3 、 创建   ArrayList<Book> list;添加5本书，
    使用 stream运算，将价格低于150元的书，按编号从低到高输出。
    提示    filter(?).sort(new BookCompartor()).forEach(...)
 */
    private static void 作业三() {
        List<Book> list = new ArrayList<>();
        Collections.addAll(list, new Book(101,"西游记",20),new Book(102,"水浒传",80),
                new Book(103,"三国演义",57),new Book(104,"红楼梦",40),new Book(105,"论语",30));
        Stream<Book> stream= list.stream();
        stream.filter(book-> book.getPrice()<150).sorted(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if (o1.getBid()<o2.getBid()){
                    return -1;
                }else if (o1.getBid()==o2.getBid()&&o1.getBname().equals(o2.getBname()))
                {
                    return 0;
                }
                return 1;
            }
        }).distinct().forEach(System.out::println);
    }
/*2 创建 TreeMap<Book,String> map;加入5本书，
            map.put(new Book(1,"西游记",100),"老白");
    要求按书的价格从高到低排序输出。
    使用两个遍历方法：  keySet遍历，entrySet遍历
 */

    private static void 作业二() {
        Map<Book,String> map  = new TreeMap<>(new BookComparable());
        map.put(new Book(1,"西游记",40),"老白");
        map.put(new Book(2,"水浒传",10),"施耐庵");
        map.put(new Book(3,"三国演义",70),"罗贯中");
        map.put(new Book(4,"红楼梦",20),"曹雪芹");
        map.put(new Book(5,"论语",500),"孔子");
        Iterator<Book> a1 = map.keySet().stream().iterator();
        while (a1.hasNext()){
            Book book = a1.next();
            String value = map.get(book);
            System.out.println(book+"="+value);
        }
        Iterator<Map.Entry<Book,String>> a2 = map.entrySet().iterator();
        while (a2.hasNext()){
            Map.Entry<Book,String> e = a2.next();
            Book book = e.getKey();
            String value = e.getValue();
            System.out.println(book+"--"+value);
        }

    }


}
